@extends('layouts.back_stage')
@section('content')
    <link rel="stylesheet" href="{{env('CDN_DOMAIN_NAME')}}/css/comeAndGo.css?v=1.0.1">
    <div class="container">
        <div class="supplier-btn">
            <a class="supplier-a" href="/admin/supplier_manager">供应商管理</a>
            <a class="supplier-a" href="javascript:;" style="background:#27c24c;">供应商类型</a>
        </div>
        <ul class="supplier-ul">
            {{--供应商类型--}}
            <li id="distributor" class="supplier-li">
                <div class="supplier-choose">
                    <a class="btn bt_a1 bt_a" href="javascript:;" onclick="delSupplier()">删除</a>
                    <a class="btn bt_a2 bt_a" href="javascript:;" onclick="amendSupplier()">修改</a>
                    <a class="btn bt_a btn-primary" href="javascript:;" onclick="addSupplier()">添加</a>
                </div>
                <div class="supplier_tab">
                    <table class="supplier" border="1" cellspacing="0" cellpadding="0">
                        <tr><td><input type="checkbox" class="allSupplier"></td><td>供应商代码</td><td>供应商类型</td><td>供应商类型代码</td><td>供应商说明</td></tr>
                        {{--<tr class='supplier_{{ $v->id }}'><td><input type='checkbox' class='supplierGou' value="{{ $v->id }}"></td><td class='supplier-code'>{{}}</td><td class='supplier-type'>{{}}</td><td class='supplier-byClass'>{{}}</td><td class='supplier-explain'>{{}}</td></tr>--}}
                    </table>
                </div>
            </li>
        </ul>
    </div>
    {{--遮罩层--}}
    <div class="mask" style="display:none;"></div>
    {{--供应商类型弹出框--}}
    <div class="provider" style="display:none;">
        <div class="provider_header">
            <span>添加信息</span>
            <button class="occlude shut">×</button>
        </div>
        <div class="popup_center">
            <input class="popup_inp np1 provider_inp" type="text" autocomplete="off" autocorrect="off" placeholder="供应商代码" />
        </div>
        <div class="popup_center">
            <input class="popup_inp np2 provider_inp" type="text" autocomplete="off" autocorrect="off" placeholder="供应商类型" />
        </div>
        <div class="popup_center">
            <input class="popup_inp np3 provider_inp" type="text" autocomplete="off" autocorrect="off" placeholder="供应商类型代码" />
        </div>
        <div class="popup_center">
            <input class="popup_inp np4 provider_inp" type="text" autocomplete="off" autocorrect="off" placeholder="供应商说明" />
        </div>
        <div class="popup_bottom">
            <input type="hidden" class="Num" value="0">
            <button class="popup_btn">确定</button>
        </div>
    </div>
    <script>
        /*拖拽*/
        function drag(id1,id2){
            var mouseOffx = 0;
            var mouseOffy = 0;
            var isDrag = false;
            $(id1).unbind(".click").on("mousedown", function (ev) {
                mouseOffx = ev.clientX - $(id1).offset().left;
                mouseOffy = ev.clientY - $(id1).offset().top;
                isDrag = true;
            })
            $(document).unbind(".click").on("mousemove", function (ev) {
                var mourseX = ev.clientX, mourseY = ev.clientY;
                var moveX = 0, moveY = 0;
                if (isDrag === true) {
                    moveX = mourseX - mouseOffx;
                    moveY = mourseY - mouseOffy;
                    var maxX = $(window).outerWidth(false) - $(id2).outerWidth(false);
                    var maxY = $(window).outerHeight(false) - $(id2).outerHeight(false);
                    moveX = Math.min(maxX, Math.max(0, moveX));
                    moveY = Math.min(maxY, Math.max(0, moveY));
                    $(id2).css({ "left": moveX, "top": moveY });
                }
            });
            $(document).unbind(".click").on("mouseup", function () {
                isDrag = false;
            });
        }
        drag(".provider_header",".provider");
/*供应商类型js*/
        /*点击添加按钮*/
        function addSupplier(){
            $(".popup_btn").addClass("submit1").removeClass("submit2")  ;
            $(".np1").val("");
            $(".np2").val("");
            $(".np3").val("");
            $(".np4").val("");
            $(".provider").show();
            $(".mask").show();
        }
        /*点击确定按钮*/
        $(document).on("click",".submit1",function(){
            var v1=$(".np1").val("");
            var v2=$(".np2").val("");
            var v3=$(".np3").val("");
            var v4=$(".np4").val("");
            if(v2==""){
                alert("请填写供应商类型");
                return false;
            }
            $(".provider").hide();
            $(".mask").hide();
            $.ajax({
                headers: {
                    'X-CSRF-TOKEN': $('meta[name="csrf_token"]').attr('content')
                },
                url:'',
                type:'post',
                data:{
                    '':v1,
                    '': v2,
                    '': v3,
                    '': v4
                },
                dataType:'json',
                success:function (data) {
                    if(data.success == true){
                        var str1="<tr class='supplier_"+data.info+"'>"+
                                "<td><input type='checkbox' class='supplierGou' value="+data.info+"></td>"+
                                "<td class='supplier-code'>"+v1+"</td>"+
                                "<td class='supplier-type'>"+v2+"</td>"+
                                "<td class='supplier-byClass'>"+v3+"</td>"+
                                "<td class='supplier-explain'>"+v4+"</td>"+
                                "</tr>"
                        $('.supplier_tab .supplier').append(str1);
                    }else{
                        alert(data.msg);
                    }
                }
            })
        });
        /*点击关闭按钮*/
        $(".occlude").click(function(){
            $(".provider").hide();
            $(".mask").hide();
        })
        /*删除*/
        function delSupplier(){
            var n=0;
            for(var i=0; i< $(".supplierGou").length;i++){
                if($(".supplierGou")[i].checked){
                    n++;
                    var id=$(".supplierGou")[i].value;
                }
            }
            if(n>0 && n<=1){
                if(confirm('确定要删除吗？')){
                    $.ajax({
                        headers: {
                            'X-CSRF-TOKEN': $('meta[name="csrf_token"]').attr('content')
                        },
                        type:'post',
                        url:'',
                        data:{
                            'id':id
                        },
                        dataType:'json',
                        success:function (data) {
                            if(data.success){
                                window.location.reload();
                            }else{
                                alert(data.msg);
                            }
                        }
                    });
                }
                return false;
            }else{
                confirm('请选取一项进行删除')
            }
        }
        /*修改*/
        function amendSupplier(){
            var m=0;
            for(var i=0; i< $(".supplierGou").length;i++){
                if($(".supplierGou")[i].checked){
                    var num=$(".supplierGou")[i].value;
                    m++;
                }
            }
            if(m<=1 && m>0){
                $(".popup_btn").addClass("submit2").removeClass("submit1")
                $(".provider").show();
                $(".mask").show();
                $(".np1").val( $('.supplier_'+num+'').children(".supplier-code").text() );
                $(".np2").val( $('.supplier_'+num+'').children(".supplier-type").text() );
                $(".np3").val( $('.supplier_'+num+'').children(".supplier-byClass").text() );
                $(".np4").val( $('.supplier_'+num+'').children(".supplier-explain").text() );

            }else{
                confirm('请选取一项进行修改')
            }
        }
        $(document).on("click",".submit2",function(){
            var m1=$(".np1").val();
            var m2=$(".np2").val();
            var m3=$(".np3").val();
            var m4=$(".np4").val();
            if(m2==""){
                alert("供应商类型不能为空");
                return false;
            }
            $(".provider").hide();
            $(".mask").hide();
            for(var i=0; i< $(".supplierGou").length;i++){
                if($(".supplierGou")[i].checked){
                    var id=$(".supplierGou")[i].value;
                }
            }
            $.ajax({
                headers: {
                    'X-CSRF-TOKEN': $('meta[name="csrf_token"]').attr('content')
                },
                type:'post',
                url:'',
                data:{
                    '':m1,
                    '': m2,
                    '': m3,
                    '': m4,
                    'id': id
                },
                dataType:'json',
                success:function (data) {
                    if(data.success){
                        window.location.reload();
                    }else{
                        alert(data.msg);
                    }
                }
            });
        })
        /*全选*/
        $(".allSupplier").click(function(){
            $(".supplierGou").prop("checked",$(this).prop("checked"));
        });
    </script>
@endsection